Grade Down (Dyadic) R←X⍒Y

Y must be a simple character array of rank greater than 0.  X must be a simple character array of rank 1 or greater.  R is a simple integer vector of shape 1↑⍴Y containing the permutation of ⍳1↑⍴Y that places the sub-arrays of Y along the first axis in descending order according to the collation sequence X.  The indices of any set of identical sub-arrays in Y occur in R in ascending order.

If X is a vector, the following identity holds:

      X⍒Y ←→ ⍒X⍳Y

A left argument of rank greater than 1 allows successive resolution of duplicate orderings in the following way.

Starting with the last axis:

 

The process is repeated using each axis in turn, from the last to the first, resolving duplicates until either no duplicates result or all axes have been exhausted.

For example, if index origin is 1:

Left argument: Right argument:
abc
ABA


ab
ac
Aa
Ac

Along last axis:

Character: Value: Ordering:
ab
ac
Aa
Ac
1 2
1 3
1 1
1 3
3
=1   <-duplicate ordering with
4
=1   <-respect to last axis.

Duplicates exist, so resolve these with respect to the first axis:

Character: Value: Ordering:
ac
Ac
1 1
2 1
2
1

So the final row ordering is:

        ab              3
        ac              2
        Aa              4
        Ac              1

That is, the order of rows is 4 2 1 3 which corresponds to a descending row sort of:

        Ac              1
        ac              2
        ab              3
        Aa              4

 

Examples

      ⍴S1
2 27
      S1
 ABCDEFGHIJKLMNOPQRSTUVWXYZ
 abcdefghijklmnopqrstuvwxyz
      S2
 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
      S3
 AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz
      S4
 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

The following results are tabulated for comparison:

X       X[S1⍒X;]    X[S2⍒X;]    X[S3⍒X;]    X[S4⍒X;]
FIRsT     TAPE        rAT         TAPE        TAPE
TAP       TAP         fIRST       TAP         TAP
RATE      RATE        TAPE        rAT         RATE
FiRST     rAT         TAP         RATE        rAT
FIRST     RAT         RATE        RAT         RAT
rAT       MAT         RAT         MAT         MAT
fIRST     fIRST       MAT         fIRST       FIRsT
TAPE      FiRST       FiRST       FiRST       FiRST
MAT       FIRsT       FIRsT       FIRsT       FIRST
RAT       FIRST       FIRST       FIRST       fIRST

⎕IO is an implicit argument of Grade Down.